package com.lql.lqlreader.androidlearning.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.google.android.material.tabs.TabLayout
import com.lql.lqlreader.R
import kotlinx.android.synthetic.main.fragment_android_learning.*
import timber.log.Timber

private const val DOUBLE_CLICK_TIME = 500 // ms

/**
 * ClassName: AndroidLearningFragment
 * Description: Android Learning
 */
class AndroidLearningFragment : Fragment() {
    private lateinit var mViewPagerAdapter: ViewPagerAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        return inflater.inflate(R.layout.fragment_android_learning, container) 是不是该这样写？
        return LayoutInflater.from(context).inflate(R.layout.fragment_android_learning, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val recommendFragment = AndroidLearningRecommendFragment()
        val systemFragment: AndroidLearningSystemFragment by lazy { AndroidLearningSystemFragment() }
        val projectFragment: AndroidLearningProjectFragment by lazy { AndroidLearningProjectFragment() }
        val fragmentList = arrayOf(recommendFragment, systemFragment, projectFragment)
        val titleList = arrayOf("推荐", "环境", "项目")

        mViewPagerAdapter = ViewPagerAdapter(activity!!.supportFragmentManager, fragmentList, titleList)

        vp_androidLearning.offscreenPageLimit = 3
        vp_androidLearning.adapter = mViewPagerAdapter
        tl_androidLearning.setupWithViewPager(vp_androidLearning)

        tl_androidLearning.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            var lastPressTime: Long = 0
            override fun onTabReselected(p0: TabLayout.Tab?) {
                if (System.currentTimeMillis() - lastPressTime < DOUBLE_CLICK_TIME) {
                    Timber.d("Invalid double click event , click position is  %s", p0?.position.toString())
                } else {
                    recommendFragment.gotoFirstPage()
                }
                lastPressTime = System.currentTimeMillis()
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }

            override fun onTabSelected(p0: TabLayout.Tab?) {
            }
        })
    }
}

// 设置适配器
class ViewPagerAdapter(fragmentManager: FragmentManager, private val fragmentList: Array<AndroidLearningBaseFragment>,
                       private val titleList: Array<String>) : FragmentStatePagerAdapter(fragmentManager){
    override fun getItem(position: Int) = fragmentList[position]

    override fun getCount() = fragmentList.size

    override fun getPageTitle(position: Int) = titleList[position]
}
